<?php

namespace App\Console\Commands;

use App\Models\Report;
use Illuminate\Console\Command;
use Carbon\Carbon;


class IncomeCountMonthFix extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'income:month-report-fix';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '收入统计月报表-fix';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();

    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $year  = intval(date('Y'));     // this year
        $month = intval(date('n')) - 1;     // this month
        $this->_mkdata($month, $year);

        // // batch 
        // for ($i = 4; $i <= 11; $i++) { 
        //     $this->_mkdata($i, $year);
        // }

    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function _mkdata($month = 0, $year = 0)
    {
        $dayStart = ($month === 1) ? 1 : 0;
        $dayEnd   = ($month === 0 || $month === 12) ? 0 : -1;

        $timeStart = mktime(0, 0, 0, $month, $dayStart, $year);
        $timeEnd   = mktime(0, 0, 0, $month+1, $dayEnd, $year);

        $dateStart = date('Y-m-d', $timeStart);
        $dateEnd   = date('Y-m-d', $timeEnd);

        $fullMonth = date('Y-m', $timeEnd);


        $this->info(sprintf('year:      %s', $year));
        $this->info(sprintf('month:     %s', $month));
        $this->info(sprintf('timeStart: %s', $timeStart));
        $this->info(sprintf('timeEnd:   %s', $timeEnd));

        $this->info(sprintf('month:     %s', $fullMonth));
        $this->info(sprintf('dateStart: %s', $dateStart));
        $this->info(sprintf('dateEnd:   %s', $dateEnd));
        // return false;

        $data = [];

        for ($i = $timeStart; $i <= $timeEnd; $i+=86400) { 

            $date = date('Y-m-d', $i);

            $this->info(date('Y-m-d H:i:s', $i));
            $this->info($date);

            $rs = Report::where('report_date', $date)
                            ->where('type', 'income_count_day_report')
                            ->first();

            if (!$rs) {
                continue;
            }

            foreach ($rs->value as $key => $item) {
                if (!isset($data[$key])) {
                    $data[$key] = 0;
                }

                $data[$key] += $item;
            }

        }

        $report = Report::where('report_date', $fullMonth)
                            ->where('type', 'income_count_month_report')
                            ->first();

        // 查询是否已存在
        if (!$report) {
            $report = new Report;
            $report->report_date = $fullMonth;
            $report->type = 'income_count_month_report';
        }

        //存入数据库
        $report->value = $data;
        $report->save();

        $this->info('月流收入统计成功');
    }
}
